﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IHandleResources.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Implementation of the interface IHandleResourcesFactory.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Communications
{
    using System.Collections.Generic;
    using Logging;

    public sealed class ResourceClientFactory : IHandleResourcesFactory
    {
        private readonly ILog log = new DebugLog();

        private readonly Dictionary<string, string> namespaces;

        private readonly Dictionary<string, IHeader> headers;

        private string username;

        private string password;

        private bool gzip;

        private JsonHandlerDefinitions jsonHandlerDefintions;
        
        public ResourceClientFactory()
            : this(new DebugLog())
        {
        }

        public ResourceClientFactory(ILog log)
        {
            this.log = log;
            this.namespaces = new Dictionary<string, string>();
            this.headers = new Dictionary<string, IHeader>();
        }
        
        public IHandleResources Create()
        {
            var resourceClient = new ResourceClient(this.log);

            if (!string.IsNullOrEmpty(this.username))
            {
                resourceClient.WithBasicAuthentication(this.username, this.password);
            }

            if (this.jsonHandlerDefintions != null)
            {
                resourceClient.WithHandlerDefinitions(this.jsonHandlerDefintions);
            }

            if (this.gzip)
            {
                resourceClient.WithGzip();
            }

            foreach (var keyValue in this.namespaces)
            {
                resourceClient.WithNamespace(keyValue.Key, keyValue.Value);
            }

            foreach (var keyValue in this.headers)
            {
                resourceClient.WithHeader(keyValue.Value);
            }

            return resourceClient;
        }

        public IHandleResourcesFactory WithNamespace(string prefix, string @namespace)
        {
            if (this.namespaces.ContainsKey(prefix))
            {
                this.namespaces[prefix] = @namespace;
            }
            else
            {
                this.namespaces.Add(prefix, @namespace);
            }

            return this;
        }

        public IHandleResourcesFactory WithBasicAuthentication(string username, string password)
        {
            this.username = username;
            this.password = password;
            return this;
        }

        public IHandleResourcesFactory WithGzip()
        {
            this.gzip = true;
            return this;
        }

        public IHandleResourcesFactory WithHandlerDefinitions(JsonHandlerDefinitions definitions)
        {
            this.jsonHandlerDefintions = definitions;
            return this;
        }

        public IHandleResourcesFactory WithHeader(IHeader header)
        {
            if (this.headers.ContainsKey(header.Name))
            {
                this.headers[header.Name] = header;
            }
            else
            {
                this.headers.Add(header.Name, header);
            }
            
            return this;
        }
    }
}